package main.java.indi.zyj.coderecord;

import main.java.indi.zyj.hot100.base.TreeNode;

public class IsBalanced {


    public boolean isBalanced(TreeNode root) {

        return getTreeHeigh(root) != -1;

    }

    private int getTreeHeigh(TreeNode root) {

        if (root == null) {
            return 0;
        }

        int left = getTreeHeigh(root.left);
        if (left == -1) {
            return -1;
        }

        int right = getTreeHeigh(root.right);
        if (right == -1) {
            return -1;
        }

        if (Math.abs(left - right) > 1) {
            return -1;
        }

        return Math.max(left, right) + 1;

    }

}
